/* Copyright (c) 2016-2020 Enfry Ltd. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Enfry. 
 * You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with Enfry.
 */
package com.enfry.validation.validators;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.enfry.validation.IValidator;
import com.enfry.validation.config.Rule;
import com.enfry.validation.validators.EqualsValidator;

/**
 * 相等匹配
 * @Title com.enfry.validation.validators.EqualsValidator
 * @author liulei
 * @date 2016年7月22日
 */
public class EqualsValidator implements IValidator {
    private static final Logger logger = LoggerFactory.getLogger(EqualsValidator.class);

    @Override
    public boolean execute(Object data, Class<?> type, Object value, Rule rule) {
        if (value == null) return false;
        String toName = rule.getParameter("target");
        if (StringUtils.isBlank(toName)) {
            logger.warn("Equals target parameter missed");
            return false;
        }

        Object toValue = null;
        try {
            toValue = PropertyUtils.getProperty(data, toName);
        } catch (Exception e) {
            logger.warn("Equals target value missed , " + toName);
        }
        return value.equals(toValue);
    }

}
